package com.tcjk.controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.tcjk.po.WebAccessToken;
import com.tcjk.service.WxInterService;

@Controller
@RequestMapping({ "/wx" })
public class WeiXinController {
	@Autowired
	WxInterService wxInterService;

	@RequestMapping(value = { "/url" }, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String url(HttpServletRequest request, HttpServletResponse response) {
		String signature = request.getParameter("signature");
		String timestamp = request.getParameter("timestamp");
		String nonce = request.getParameter("nonce");
		String echostr = request.getParameter("echostr");
		System.out.println("signature====" + signature);
		System.out.println("timestamp====" + timestamp);
		System.out.println("nonce====" + nonce);
		System.out.println("echostr====" + echostr);
		try {
			getPostData(request);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return echostr;
	}

	@RequestMapping(value = "/createMunm", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String createMunm() {
		wxInterService.menuCreat();
		return "公众号菜单创建成功";
	}

	public String getPostData(HttpServletRequest request) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
		String line = null;
		StringBuilder sb = new StringBuilder();
		while ((line = br.readLine()) != null) {
			sb.append(line);
			System.out.print(line);
		}
		System.out.println();
		return sb.toString();
	}

	@RequestMapping(value = "/index")
	public String index(@RequestParam(value = "code", defaultValue = "code") String code) {
		WebAccessToken wat = wxInterService.getWebAccessToken(code);
		return "redirect:/index.jsp";
	}
	
	@RequestMapping(value = "/main")
	public String main() {
		return "redirect:/main.jsp";
	}
}
